﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AionCxxLibrary.Packets;
using AionCxxLibrary.Network;
using AionCxxLibrary.Utilities;
using AionCxxLibrary.Enums;
using System.Net;
using System.Configuration;
using WorldServer.Services;
using WorldServer.Models;

namespace WorldServer.Packets.Server
{
    public class SPkt_CryptKey : AionServerPacket
    {
        public override void WritePacket()
        {
            WorldConnection con = (WorldConnection)Connection;
            int key = ((WorldCrypter)con.Crypter)._crypt.enableKey();
            WriteInt(key);
        }
    }

    public class SPkt_ServerVersion : AionServerPacket
    {
        public override void WritePacket()
        {
            WriteByte(0);
            WriteByte(2); // ServerID
            WriteInt(0x01b1b1);
            WriteInt(0x01b1aa);
            WriteInt(0);
            WriteInt(0x01b21a);
            WriteInt(0x4edf6585);
            WriteByte(0);
            WriteByte(2); // CountryCode
            WriteByte(0);
            WriteByte(1); // Server Mode
            WriteInt((int)Utility.DateTimeToTimestamp(DateTime.Now));
            WriteShort(0x15e);
            WriteShort(0xa01);
            WriteShort(0xa01);
            WriteShort(0x370a);
            WriteShort(2);
            WriteByte(0x14);
            WriteByte(1);
            WriteInt(0);
            WriteByte(1);
            WriteShort(0);

            WriteBytes(Utility.IPAddressToByteArray("188.165.229.196"));
            WriteShort(10241);
        }
    }

    public class SPkt_Login : AionServerPacket
    {
        public override void WritePacket()
        {
            WriteInt(Connection.account == null ? 1 : 0);
            WriteString(Connection.account == null ? "" : Connection.account.Name.ToLower());
        }
    }

    public class SPkt_Time : AionServerPacket
    {
        int time;

        public SPkt_Time(int clienttime)
        {
            time = clienttime;
        }

        public override void WritePacket()
        {
            int now = (int)Utility.CurrentMillis();
            WriteInt(now); // Server Time
            WriteInt(time); // Client Time
            Log.Debug("Client Time " + time + " Server " + now);
        }
    }

    public class SPkt_AtreiaClock : AionServerPacket
    {
        public override void WritePacket()
        {
            WriteInt((int)(Utility.DateTimeToTimestamp(DateTime.Now) / 10000));
        }
    }

    public class SPkt_ChooseServer : AionServerPacket
    {
        int key;
        public SPkt_ChooseServer(int reconnectKey)
        {
            key = reconnectKey;
        }
        public override void WritePacket()
        {
            WriteByte(0);
            WriteInt(key);
        }
    }

    public class SPkt_KeepAlive : AionServerPacket
    {
        public override void WritePacket()
        {
            WriteShort(0);
        }
    }

    public class SPkt_EnterWorldCheck : AionServerPacket
    {
        public override void WritePacket()
        {
            WriteInt(0);
        }
    }

    public  class SPkt_EnterWorldAccepted : AionServerPacket
    {
        public override void WritePacket()
        {
            WriteShort(0);
            WriteByte(0);
        }
    }

    public class SPkt_ClientSettings : AionServerPacket
    {
        byte type;
        byte[] data;

        public SPkt_ClientSettings(byte type, byte[] data)
        {
            this.type = type;
            this.data = data;
        }

        public override void WritePacket()
        {
            WriteShort(type);
            WriteByte(0x1c);
            WriteBytes(data);
        }
    }

    public class SPkt_WorldQuit : AionServerPacket
    {
        bool plasticSurgery;

        public SPkt_WorldQuit(bool GoingToPlasticSurgery)
        {
            plasticSurgery = GoingToPlasticSurgery;
        }

        public override void WritePacket()
        {
            WriteInt(plasticSurgery ? 2 : 1);
            WriteByte(0);
        }
    }

    public class SPkt_UIQuestion : AionServerPacket
    {
        int sender;
        int message;
        object[] parameters;
        public SPkt_UIQuestion(int senderId, int messageId, params object[] parameters)
        {
            this.sender = senderId;
            this.message = messageId;
            this.parameters = parameters;
        }

        public override void WritePacket()
        {
            WriteInt(message);
            foreach (object p in parameters)
            {
                if (p is AionDescription)
                {
                    WriteShort(0x24);
                    WriteInt((p as AionDescription).DescriptionID);
                    WriteShort(0);
                }
                else
                    WriteString(p.ToString());
            }
            // Guardian Stone Activation Window
            if (message == 160027)
            {
                WriteLong(0);
                WriteLong(0);
                WriteInt(0);
                WriteShort(0);
                WriteByte(0);
            }
            // Artifact Activation Window
            else if (message == 160028)
            {
                WriteLong(0);
                WriteLong(0);
                WriteShort(0);
                WriteByte(0);
            }
            else
            {
                WriteInt(0);
                WriteShort(0);
                WriteByte(1);
                WriteInt(sender);
                WriteInt(6);
            }
        }

    }

}
